package com.game.centerserver.infrastructure.service.impl;

import com.game.centerserver.infrastructure.entity.Port;
import com.game.centerserver.infrastructure.dao.PortDao;
import com.game.centerserver.infrastructure.service.PortService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;
//import org.springframework.data.domain.Page;
//import org.springframework.data.domain.PageImpl;
//import org.springframework.data.domain.PageRequest;
/**
 * (Port)表服务实现类
 *
 * @author makejava
 * @since 2024-12-16 22:16:07
 */
@Service("portService")
public class PortServiceImpl implements PortService {
    @Resource
    private PortDao portDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Port queryById(Integer id) {
        return this.portDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param port 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
//    @Override
//    public Page<Port> queryByPage(Port port, PageRequest pageRequest) {
//        long total = this.portDao.count(port);
//        return new PageImpl<>(this.portDao.queryAllByLimit(port, pageRequest), pageRequest, total);
//    }

    /**
     * 新增数据
     *
     * @param port 实例对象
     * @return 实例对象
     */
    @Override
    public Port insert(Port port) {
        if (port.getPort() == null || port.getIp() == null ||
            port.getType() == null || port.getUsable() == null) {
            return null;
        }

        Port queryPort = new Port();
        queryPort.setPort(port.getPort());
        queryPort.setIp(port.getIp());
        queryPort.setType(port.getType());

        List<Port> reslist = this.portDao.query(queryPort);
        if (reslist.size() > 0) {
            return null;
        }
        this.portDao.insert(port);
        queryPort.setType(port.getType());
        reslist = this.portDao.query(queryPort);

        Port res = new Port();
        res.setId(reslist.get(0).getId());
        res.setIp(reslist.get(0).getIp());
        res.setPort(reslist.get(0).getPort());
        res.setType(reslist.get(0).getType());
        res.setUsable(reslist.get(0).getUsable());
        return res;
    }

    /**
     * 修改数据
     *
     * @param port 实例对象
     * @return 实例对象
     */
    @Override
    public Port update(Port port) {
        this.portDao.update(port);
        return this.queryById(port.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.portDao.deleteById(id) > 0;
    }

    @Override
    public List<Port> query(Port port) {
        return portDao.query(port);
    }
}
